package cn.bdqn.hospital.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 饿汉单例模式
 */
@WebFilter("*.do")
public class CharsetFilter implements Filter {
    /**
     * 初始化方法
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("字符过滤器启动完成。。。");
    }
    /**
     * 执行过滤器执行的功能
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("doFilter处理请求和响应");
        servletRequest.setCharacterEncoding("UTF-8");      // 设置请求字符集
        servletResponse.setContentType("text/html;charset=UTF-8");  // 设置响应字符集
        filterChain.doFilter(servletRequest, servletResponse);      // 请求和响应的对象引用
    }
    /**
     * 销毁过滤器
     */
    @Override
    public void destroy() {
        System.out.println("字符过滤器销毁内存。。。");
    }
}
